package util

import (
	"mall/common/globalkey"
	"strings"

	"mall/service/backend/rpc/backendclient"
)

func GetGenderText(gender int32) (res string) {
	switch gender {
	case 1:
		res = "男"
	case 2:
		res = "女"
	default:
		res = "-"
	}

	return
}

func GetStatusText(status int32) (res string) {
	switch status {
	case 1:
		res = "启用"
	case 2:
		res = "注销"
	case 3:
		res = "删除"
	default:
		res = "禁用"
	}

	return
}

func ParseSorter(sorter string) (res []*backendclient.OrderBy) {
	sorters := strings.Split(sorter, ";")
	if len(sorters) == 0 {
		return
	}

	for _, orderBy := range sorters {
		orderBys := strings.Split(orderBy, ":")
		if len(orderBys) != 2 {
			continue
		}

		res = append(res, &backendclient.OrderBy{
			Field: orderBys[0],
			Order: orderBys[1],
		})
	}

	return
}

func ParseFilter(filter string) (res []*backendclient.FilterBy) {
	filters := strings.Split(filter, ";")
	if len(filters) == 0 {
		return
	}

	for _, filterBy := range filters {
		filterBys := strings.Split(filterBy, ":")
		if len(filterBys) != 2 {
			continue
		}

		res = append(res, &backendclient.FilterBy{
			Field: filterBys[0],
			In:    filterBys[1],
		})
	}

	return
}

type UserOption struct {
	CanDisable       bool
	CanEnable        bool
	CanForceDelete   bool
	CanRemove        bool
	CanRestore       bool
	CanResetPassword bool
}

func GetUserOption(status int32, isSuper bool) *UserOption {
	res := &UserOption{}
	if isSuper {
		return res
	}

	switch status {
	case 1:
		res.CanDisable = true
		res.CanForceDelete = true
		res.CanRemove = true
		res.CanResetPassword = true
	case 2:
		res.CanForceDelete = true
		res.CanRestore = true
	case 3:
		res.CanForceDelete = true
		res.CanRestore = true
	default:
		res.CanEnable = true
		res.CanForceDelete = true
		res.CanRemove = true
	}

	return res
}

type Option struct {
	CanDisable     bool
	CanEnable      bool
	CanForceDelete bool
	CanRemove      bool
	CanRestore     bool
}

func GetOptionByStatus(status int32) *Option {
	res := &Option{}

	switch status {
	case 1:
		res.CanDisable = true
		res.CanForceDelete = true
		res.CanRemove = true
	case 2:
		res.CanForceDelete = true
		res.CanRestore = true
	case 3:
		res.CanForceDelete = true
		res.CanRestore = true
	default:
		res.CanEnable = true
		res.CanForceDelete = true
		res.CanRemove = true
	}

	return res
}

// 线上交易状态
// 0：未创建交易；
// 1：待付款；
// 2：待发货；
// 3：已完成；
// 4：已关闭；
// 5：已发货待确认；
// 6：已签收；

// 明细线上状态
// 0：未创建订单；
// 1：待付款；
// 2：待发货；
// 3：已完成；
// 4：已关闭；
// 5：已发货待确认；
// 6：已签收；
func ErpOrderStatusToOrderStatus(orderStatus uint32) uint32 {
	switch orderStatus {
	//打单配货
	case 0:
		return globalkey.OrderTradeStatePaid
	//打单配货
	case 1:
		return globalkey.OrderTradeStateShipping
	//待发货
	case 4:
		return globalkey.OrderTradeStatePaid

	case 8:
		return globalkey.OrderTradeStateShipped
	case 13:
		return globalkey.OrderTradeStateClosed
	case 5:
		return globalkey.OrderTradeStateDelivered
	case 6:
		return globalkey.OrderTradeStateConfirmed
	default:
		return 0
	}
}

func OrderStatusToErpOrderStatus(orderStatus *uint32) uint32 {
	if orderStatus == nil {
		return 0
	}

	switch *orderStatus {
	case globalkey.OrderTradeStatePaid:
		return 2
	case globalkey.OrderTradeStateConfirmed, globalkey.OrderTradeStateAutoConfirmed:
		return 6
	case globalkey.OrderTradeStateFulfilled:
		return 3
	case globalkey.OrderTradeStateClosed:
		return 4
	case globalkey.OrderTradeStateDelivered:
		return 5
	default:
		return 0
	}
}

// 售后单状态:1: 换货待处理 2: 待买家退货 3: 买家已退货，待收货 4: 待买家修改 5: 换货关闭 6: 换货成功 7: 待发出换货商品（不能为空）  integer
func OrderRefundStatusToErpOrderStatus(orderRefundStatus *uint32) uint32 {
	if orderRefundStatus == nil {
		return 0
	}

	switch *orderRefundStatus {
	case globalkey.AfterSaleStatusPending:
		return 1
	case globalkey.AfterSaleStatusAwaitReturn:
		return 2
	case globalkey.AfterSaleStatusAwaitConfirm:
		return 3
	case globalkey.AfterSaleStatusRejected:
		return 4
	case globalkey.AfterSaleStatusClosed:
		return 5
	case globalkey.AfterSaleStatusSuccess:
		return 6
	case globalkey.AfterSaleStatusAwaitBarter:
		return 7
	default:
		return 0
	}
}
